<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 明细管理
 * @package app\recorddetail
 * @author LJQ<245369209@qq.com>
 */
class Recorddetail extends Api

{

    /**
   *  明细列表
   * @param string 
   * @return Json
   */

	public function recorddetailList($recordId=null,$recorddetailId=null){
        if (empty($recordId)) {
            return json(['code'=>0,'msg'=>'没有找到预案']);
        }

        $where['rescueExecId']=['eq',$recordId];
        if ($recorddetailId) {
            $where['id']=['eq',$recorddetailId];
        }
        //分页参数
        if(($limit = input('limit')) && ($page = input('page'))){
            $startLimit =  ($page-1)*$limit;
            // 执行明细列表
            $info =collection(model('plan/recorddetail')->where($where)->limit($startLimit.','.$limit)->order('stepIndex')->select())->toArray();
        }else{
            //执行明细列表
            $info =collection(model('plan/recorddetail')->where($where)->order('stepIndex')->select())->toArray();
        }

        //总数
        $count = model('plan/recorddetail')->where($where)->count('id');

        if ($info && $count == 0) {
            return json(['code'=>0,'msg'=>'没有找到执行明细']);
        }

        if ($count > 0){
            return json(['code'=>1,'msg'=>'执行明细','count'=>$count,'data'=>$info]);
        }
        return json(['code'=>0,'msg'=>'数据异常']);
    }
   	  
   	      //添加明细
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('plan/recorddetail');
          $result = $model->change($data);
          if (false != $result) {
            setlog('recorddetailadd','record/recorddetail',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'明细添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'明细添加失败,请检查']);
          }
      }

   	

   /**
   * 修改明细
   * @param  string recorddetailId 明细id,
   * @return Json
   */
   	public function edit($data,$recorddetailId){
        
          if (empty($recorddetailId)) {
              return json(['code'=>0,'msg'=>'明细不存在,请检查']);
          }
          $model = model('plan/recorddetail');     
          if ($model->change($data)) {
            setlog('recorddetailupdate','record/recorddetail',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'明细修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'明细明细修改失败,请检查']);
            }
    }
    /**
   *  删除明细
   * @param string recorddetailId 明细id,
   * @param Json
   */
     
    public function del($recorddetailId) {
        $recorddetailId = input('recorddetailId',0,'intval');
        $model = model('plan/recorddetail');
      	if (empty ( $recorddetailId )||empty(($model->where(array('id'=>$recorddetailId))->find()))) {
           	return  json(['code'=>0,'msg'=>'明细id不存在,请检查']);
        }
        if($model->destroy(['id'=>$recorddetailId]) !== FALSE){
             setlog('recorddetaildel','record/recorddetail',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'明细删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'明细删除失败,请检查']);
       }
    }

     
   /**
   *   查询明细
   * @param keyword明细名称
   * @return Json
   */
   	public function search($typeId=null,$enterpriseId=null){
   		$where = [] ;

		if($typeId){
			
			$where['typeId'] = ['eq',$typeId];
		}

                if($enterpriseId){
      
                  $where['unitId'] = ['eq',$enterpriseId];
            }




   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//明细列表
			$info =collection(model('plan')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//明细列表
			$info =collection(model('plan')->with('enterprise')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('plan                                                                                                                                                                                                                                  ')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到明细','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'明细','count'=>$count,'data'=>$info]);
       	}
      }
}